package com.taotao.service.impl;

import com.taotao.common.utils.JsonUtils;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.pojo.TbItemParamItemExample;
import com.taotao.service.ItemParamItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Package com.taotao.service.impl
 * @Description:
 * @Author elwyn
 * @Date 2017/9/3 18:24
 * @Email elonyong@163.com
 */
@Service
public class ItemParamItemServiceImpl implements ItemParamItemService {
    @Autowired
    TbItemParamItemMapper tbItemParamItemMapper;

    @Override
    public String getItemParamByItemId(Long itemId) {
        TbItemParamItemExample tbItemParamItemExample=new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = tbItemParamItemExample.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> tbItemParamItems = tbItemParamItemMapper.selectByExampleWithBLOBs(tbItemParamItemExample);
        if (tbItemParamItems == null || tbItemParamItems.isEmpty()) {
            return "";
        }
        TbItemParamItem tbItemParamItem=tbItemParamItems.get(0);
        String paramData = tbItemParamItem.getParamData();
        List<Map> jsonList = JsonUtils.jsonToList(paramData, Map.class);
        StringBuilder sb = new StringBuilder();
        sb.append("<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"0\" class=\"Ptable\">\n");
        sb.append("    <tbody>\n");
        for(Map m1:jsonList) {
            sb.append("        <tr>\n");
            sb.append("            <th class=\"tdTitle\" colspan=\"2\">").append(m1.get("group")).append("</th>\n");
            sb.append("        </tr>\n");
            List<Map> list2 = (List<Map>) m1.get("params");
            for(Map m2:list2) {
                sb.append("        <tr>\n");
                sb.append("            <td class=\"tdTitle\">").append(m2.get("k")).append("</td>\n");
                sb.append("            <td>").append(m2.get("v")).append("</td>\n");
                sb.append("        </tr>\n");
            }
        }
        sb.append("    </tbody>\n");
        sb.append("</table>");
        return sb.toString();
    }
}
